/*
 * ModelErrorException.java
 *
 * Created on August 1, 2006, 10:44 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.smallx.xproc;

import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.net.URI;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import org.infoset.xml.Element;
import org.infoset.xml.Location;

/**
 *
 * @author alex
 */
public class ModelErrorException extends Exception {

   public class Error implements Location {
      String msg;
      URI baseURI;
      int line;
      int column;
      Error(String msg,URI baseURI,int line,int column) {
         this.msg = msg;
         this.baseURI = baseURI;
         this.line = line;
         this.column = column;
      }
      
      public String getMessage() {
         return msg;
      }
      
      public URI getBaseURI() {
         return baseURI;
      }
      
      public int getLine() {
         return line;
      }
      
      public void setLine(int line) {
         this.line = line;
      }
      
      public int getColumn() {
         return column;
      }
      
      public void setColumn(int column) {
         this.column = column;
      }
   }
   /** Creates a new instance of ModelErrorException */
   ModelItem item;
   List<Error> errors;
   ModelErrorException(String msg) {
      this(msg,null,null);
   }
   ModelErrorException(String msg,Throwable cause) {
      this(msg,null,cause);
   }
   ModelErrorException(String msg,ModelItem item) {
      this(msg,item,null);
   }
   ModelErrorException(String msg,ModelItem item,Throwable cause) {
      super(msg,cause);
      this.item = item;
      this.errors = null;
   }
   
   public ModelItem getModelItem() {
      return item;
   }
   
   public Iterator<Error> getErrors() {
      if (errors!=null) {
         return errors.iterator(); 
      } else {
         List<Error> toReturn = Collections.emptyList();
         return toReturn.iterator();
      }
   }
   
   public void addError(String msg) {
      if (errors==null) {
         errors = new ArrayList<Error>();
      }
      errors.add(new Error(msg,null,0,0));
   }
   
   public void addError(String msg,Element location) {
      if (errors==null) {
         errors = new ArrayList<Error>();
      }
      errors.add(new Error(msg,location.getBaseURI(),location.getLine(),location.getColumn()));
   }
   
   public void addError(String msg,URI baseURI,Location location) {
      if (errors==null) {
         errors = new ArrayList<Error>();
      }
      errors.add(new Error(msg,baseURI,location.getLine(),location.getColumn()));
   }
   
   public void addError(String msg,URI baseURI,int line,int column) {
      if (errors==null) {
         errors = new ArrayList<Error>();
      }
      errors.add(new Error(msg,baseURI,line,column));
   }
   
   public String getUnformattedMessage() {
      return super.getMessage();
   }

   public String getMessage() {
      StringBuilder builder = new StringBuilder();
      if (item!=null && item.getBaseURI()!=null) {
         builder.append(item.getBaseURI()+":"+item.getLine()+":"+item.getColumn()+' ');
      }
      builder.append(super.getMessage());
      if (errors!=null && errors.size()>0) {
         for (Error error : errors) {
            builder.append('\n');
            if (error.getBaseURI()!=null) {
               builder.append(error.getBaseURI()+":"+error.getLine()+":"+error.getColumn()+' ');
            }
            builder.append(error.getMessage());
         }
      }
      return builder.toString();
   }
}
